package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;

//    @Autowired
//    private GroupService groupService;

    /**
     * 根据分类id查询对应的规格参数组集合
     *
     * @param cid
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategory(
            @RequestParam("id") Long cid) {

        return ResponseEntity.ok(this.specGroupService.querySpecGroupByCategory(cid));
    }


//    /**
//     * 根据规格参数组id，查询对应得组内参数
//     *
//     * @param gid
//     * @return
//     */
//    @GetMapping("params")
//    public ResponseEntity<List<SpecParamDTO>> querySpecParamByGroup(
//            @RequestParam("groupId") Long gid) {
//
//        return ResponseEntity.ok(this.specParamService.querySpecParamByGroup(gid));
//    }


    /**
     * 新增规格组
     *
     * @param groupDTO 规格组信息
     * @return 无
     */
    @PostMapping("group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO groupDTO) {
        // 新增规格组
        specGroupService.save(groupDTO.toEntity(SpecGroup.class));
        // 返回结果
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 修改规格组
     *
     * @param groupDTO 规格组信息
     * @return 无
     */
    @PutMapping("group")
    public ResponseEntity<Void> updateGroup(
            @RequestBody SpecGroupDTO groupDTO) {
        // 更新规格组
        specGroupService.updateById(groupDTO.toEntity(SpecGroup.class));
        // 返回结果
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 新增规格参数
     *
     * @param paramDTO 规格组信息
     * @return 无
     */
    @PostMapping("param")
    public ResponseEntity<Void> saveParam(@RequestBody SpecParamDTO paramDTO) {

        specParamService.save(paramDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 修改规格参数
     *
     * @param paramDTO 规格组信息
     * @return 无
     */
    @PutMapping("param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO paramDTO) {

        specParamService.updateById(paramDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 根据商品分类id查询规格组及组内参数
     *
     * @return 规格组集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") Long categoryId) {
        return ResponseEntity.ok(specGroupService.querySpecList(categoryId));
    }


    /**
     * 查询规格参数集合
     * @param categoryId 分类id
     * @param groupId 规格组id
     * @param searching 是否搜索
     * @return 参数集合
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching
    ){
        return ResponseEntity.ok(specParamService.queryParams(categoryId, groupId, searching));
    }
}
